home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / boot tools / rand / rand.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  926b  |  48 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. #define LINELENGTH 132
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.    long r, line = 0, lines = 0;
  11.    FILE *cfile;
  12.    char *buffer = (char *) malloc(LINELENGTH);
  13.  
  14.    if (argc > 1)
  15.    { 
  16.        if (!(cfile = fopen(argv[1], "r"))) 
  17.        {
  18.           printf("%s: Couldn't open file, \"%s\".\n", argv[0], argv[1]);
  19.           exit(3);
  20.        }
  21.    }
  22.    else if (!(cfile = fopen("s:random-sequences", "r"))) 
  23.    {
  24.       printf("%s: Couldn't open file, \"s:random-sequences\".\n", argv[0]);
  25.       exit(3);
  26.    }
  27.  
  28.    while (fgets(buffer, LINELENGTH, cfile) != NULL) lines++;
  29.  
  30.    srand(time(NULL));
  31.  
  32.    r = rand() % lines;
  33.  
  34.    fseek(cfile, 0, SEEK_SET);
  35.  
  36.    while (fgets(buffer, LINELENGTH, cfile) != NULL)
  37.    {
  38.       if (line++ >= r && strlen(buffer) < LINELENGTH)
  39.       {
  40.          system(buffer);
  41.          break;
  42.       }
  43.    }
  44.  
  45.    fclose(cfile);
  46.    free(buffer);
  47. }
  48.